package com.techbrand.lb7.web.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class LandlinePhoneValidator implements Validator {
	
	@SuppressWarnings("rawtypes")
	public boolean supports(Class clazz) {
		return String.class.isAssignableFrom(clazz);
	}
	
	public void validate(Object obj, Errors errors) {
		String landlinePhone = (String) obj;
		
		Pattern pattern = Pattern.compile("\\(\\d{1,5}\\)\\d{7}");
		Matcher matcher = pattern.matcher(landlinePhone);
		
		if (!matcher.matches()) {
			errors.rejectValue("phone", "landline.invalid");
		}
	}
}
